package com.itic.market.system.user.service;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.market.system.phonecode.persistence.dao.PhoneCodeDao;
import com.itic.market.system.phonecode.persistence.model.PhoneCode;
import com.itic.market.system.phonecode.persistence.model.PhoneCodeExample;
import com.itic.market.system.profitmoney.persistence.dao.ProfitMoneyDao;
import com.itic.market.system.profitmoney.persistence.model.ProfitMoney;
import com.itic.market.system.markettoken.persistence.dao.MarketTokenDao;
import com.itic.market.system.markettoken.persistence.model.MarketToken;
import com.itic.market.system.tixian.persistence.dao.TixianDao;
import com.itic.market.system.tixian.persistence.model.Tixian;
import com.itic.market.system.user.persistence.dao.MarketUserDao;
import com.itic.market.system.user.persistence.model.MarketUser;
import com.itic.market.system.user.persistence.model.MarketUserExample;


@Service
@Transactional
public class MarketUserService
     extends CrudService<MarketUserDao,MarketUser,MarketUserExample> {
	@Autowired
	private MarketTokenDao marketTokenDao;
    @Autowired
    private MarketUserDao marketUserDao;
    @Autowired
    private PhoneCodeDao phoneCodeDao;
    @Autowired
    private TixianDao tixianDao;
    @Autowired
    private ProfitMoneyDao profitMoneyDao;

    public MarketUser getUserByInvitingCode(String invitingCode){
    	return marketUserDao.getUserByInvitingCode(invitingCode);
    }


	public String regUser(MarketUser regUser, MarketToken createToken,MarketUser invitingUser,MarketUser topInviting) {
		marketTokenDao.insertSelective(createToken);
		marketUserDao.insertSelective(regUser);
		if( null != invitingUser ){
			marketUserDao.updateByPrimaryKeySelective(invitingUser);
		}
		if( null != topInviting ){
			marketUserDao.updateByPrimaryKeySelective(topInviting);
		}
		return SUCCESS;
	}


	public String bindPhoneAndDelPhoneCode(MarketUser user, String code) {
		//删除验证码
		PhoneCodeExample example = new PhoneCodeExample();
		example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andCodeEqualTo(code);
		PhoneCode phoneCode = new PhoneCode();
		phoneCode.setIsEffective(DBConst.FALSE);
		phoneCodeDao.updateByExampleSelective(phoneCode, example);
		
		marketUserDao.updateByPrimaryKeySelective(user);
		return SUCCESS;
	}


	public String tixianCreate(MarketUser user, Tixian tixian) {
		marketUserDao.updateByPrimaryKeySelective(user);
		tixianDao.insert(tixian);
		return SUCCESS;
	}


	public MarketUser getUserByOpenid(String openid) {
		return marketUserDao.getUserByOpenid(openid);
	}


	public MarketUser getUserByUnionId(String unionId) {
		return marketUserDao.getUserByUnionId(unionId);
	}


	public MarketUser getUserByName(String userNickName) {
		return marketUserDao.getUserByName(userNickName);
	}


	public String bind(MarketUser marketUser, MarketUser user1, MarketUser user2) {
		marketUserDao.updateByPrimaryKeySelective(marketUser);
		marketUserDao.updateByPrimaryKeySelective(user1);
		marketUserDao.updateByPrimaryKeySelective(user2);
		return SUCCESS;
	}


	public String editMoney(MarketUser user, ProfitMoney profitMoney) {
		marketUserDao.updateByPrimaryKeySelective(user);
		profitMoneyDao.insert(profitMoney);
		return SUCCESS;
	}


	public int countDownDown(String id) {
		return marketUserDao.countDownDown(id);
	}


	public List<MarketUser> findDownDown(String id) {
		return marketUserDao.findDownDown(id);
	}
    
}